*Change working directory
cd "/Users/gehlbach/Dropbox/Projects/Handbook paper/Data"
clear matrix
clear
set mem 3000m
set matsize 2000


/* LOAD DATA */

*full4.dta:
  *linearly interpolates capital stock from adjacent years for any firm missing that variable in 1996, 1997, or 2002, regardless of region
  *linearly interpolates employment from adjacent years for firms in region 1163 in 2000
  *interpolates capital stock for all firms in 2005 as capital stock in 2004

use full4, clear

*for region estimates:
  *drop obs in 2005
  *drop obs in regions with no region-level data

drop if year == 105
drop if ter == 1126 | ter == 1177 | ter == 1196 

*drop outliers

gen cap = exp(logcap)
gen emp = exp(logemp)
gen out = exp(lsale)
*
foreach x of varlist cap emp out {
* 
by okpo, sort: gen last=1 if `x'~=. & f.`x'==.
by okpo, sort: gen first=1 if `x'~=. & l.`x'==.
* 
by okpo: gen out1=1 if `x'/l.`x'>5 & f.`x'/`x'<0.2 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out2=1 if `x'/l.`x'<0.2 & f.`x'/`x'>5 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out3=1 if f.`x'/`x'>10 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out4=1 if f.`x'/`x'<0.1 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out5=1 if `x'/l.`x'>10 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
by okpo: gen out6=1 if `x'/l.`x'<0.1 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
* 
gen outlier`x'=1 if out1==1 | out2==1 | out3==1 | out4==1 | out5==1 |out6==1
drop out1 out2 out3 out4 out5 out6
drop last first
}
*
drop if outliercap == 1
drop if outlieremp == 1
drop if outlierout == 1

*drop missing values (do this after dropping outliers so that can take advantage of time series where exists)

drop if missing(year,ter,lsale,logemp,logcap,ind,ldo,lfo,lho,lmo)


/* VARIABLE CONSTRUCTION BEFORE DETRENDING */

*region dummies
foreach i of numlist 1 3/5 7 8 10/12 14 15 17/20 22 24/25 27/30 32/34 36/38 40/42 44/47 49 50 52/54 56/58 60 61 63/66 68/71 73 75 76 78/95 97/99 {
gen reg`i' = 1 if ter == 1100 + `i'
replace reg`i' = 0 if ter ~= 1100 + `i'
}

*year dummies
tab year, gen(y)
local j=1
forval i=1/20 {
local j=`i'+84
rename y`i' y`j'
label variable y`j' `""'
}

*ldo*year interactions (no privatizations until 1993)
foreach i of numlist 94/104 {
gen ldo_y`i' = ldo*y`i'
}

*ldo*industry*year interactions 
  *demean industry on annual basis to facilitate interpretation of regional effects
forv i = 1/10 {
gen y_ind`i'mean = .
foreach j of numlist 85/104 {
egen temp = mean(ind`i') if year == `j'
replace y_ind`i'mean = temp if year == `j'
drop temp
}
}
forv i = 1/10 {
gen y_dmind`i' = ind`i'-y_ind`i'mean
}
  *interactions
foreach i of numlist 1/10 {
foreach j of numlist 94/104 {
gen ldo_ind`i'_y`j' = ldo_y`j'*y_dmind`i'
}
}

save priv_region, replace


/* DETRENDING */

tsset okpo year
DETREND lsale logemp logcap ldo* lfo e1-e10 c1-c10 y85-y104, by(okpo) time(year)
rename _DTlsale dtlsale
rename _DTlogcap dtlogcap
rename _DTlogemp dtlogemp
rename _DTldo dtldo
forv i = 94/104 {
rename _DTldo_y`i' dtldo_y`i'
}
rename _DTlfo dtlfo
forv i = 1/10 {
rename _DTe`i' dte`i'
}
forv i = 1/10 {
rename _DTc`i' dtc`i'
}
foreach i of numlist 85/104 {
rename _DTy`i' dty`i'
}
foreach i of numlist 1/10 {
foreach j of numlist 94/104 {
rename _DTldo_ind`i'_y`j' dtldo_ind`i'_y`j' 
}
}

save dtwork_region, replace


/* VARIABLE CONSTRUCTION AFTER DETRENDING */

*dt_industry*year interactions
foreach i of numlist 85/104 {
forv j = 1/10 {
gen dtind`j'_y`i' = ind`j'*dty`i'
}
}

*dt_ldo*region interactions
foreach j of numlist 1 3/5 7 8 10/12 14 15 17/20 22 24/25 27/30 32/34 36/38 40/42 44/47 49 50 52/54 56/58 60 61 63/66 68/71 73 75 76 78/95 97/99 {
gen dtldo_reg`j' = dtldo*reg`j'
}

*dt_ldo*region*year interactions
foreach i of numlist 1 3/5 7 8 10/12 14 15 17/20 22 24/25 27/30 32/34 36/38 40/42 44/47 49 50 52/54 56/58 60 61 63/66 68/71 73 75 76 78/95 97/99 {
foreach j of numlist 94/104 {
gen dtldo_reg`i'_y`j' = dtldo_y`j'*reg`i'
}
}

*dt_ldo*industry interactions
  *demean industry to facilitate interpretation of regional effects
forv i = 1/10 {
egen ind`i'mean = mean(ind`i')
}
forv i = 1/10 {
gen dmind`i' = ind`i'-ind`i'mean
}
  *interactions
forv i = 1/10 {
gen dtldo_dmind`i' = dtldo*dmind`i'
}

save interwork_region, replace 


/* REGION EFFECTS */

reg dtlsale dtldo_reg1-dtldo_reg99 dtldo_dmind* dtlfo dte* dtc* dtind*_y*, cluster(okpo)
matrix b = e(b)'
matrix privDT = b[1..77,1]
matrix V = e(V)
matrix VprivDT = V[1..77,1..77]
matrix SE2privDT = vecdiag(VprivDT)'
svmat privDT
svmat VprivDT
svmat SE2privDT
rename privDT1 privDT
rename SE2privDT1 SE2privDT
keep in 1/77
order privDT SE2privDT VprivDT* 
keep privDT-VprivDT77
save DTmatrix, replace


/* REGIONAL DYNAMICS (controlling for sector dynamics) */

use interwork_region, clear
reg dtlsale dtldo_reg*_y* dtldo_ind1_y94-dtldo_ind10_y104 dtlfo dte* dtc* dtind*_y*, cluster(okpo)
matrix b = e(b)'
matrix privDT = b[1..847,1]
matrix V = e(V)
matrix VprivDT = V[1..847,1..847]
matrix SE2privDT = vecdiag(VprivDT)'
svmat privDT
svmat VprivDT
svmat SE2privDT
rename privDT1 privDT
rename SE2privDT1 SE2privDT
keep in 1/847 
order privDT SE2privDT VprivDT*
keep privDT SE2privDT VprivDT*
save privpanelDT, replace

*illustrate regional dynamics

clear matrix
use terlist, clear
expand 11
sort ter
egen year = fill(4(1)14 4(1)14)
sort ter year
merge using privpanelDT
drop _merge
sort ter year 
merge ter year using regional
drop if year < 4
drop _m
replace year = year + 1990
scatter privDT year if ter <= 1115, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1115 & ter <= 1130, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1130 & ter <= 1145, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1145 & ter <= 1160, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1160 & ter <= 1175, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1175 & ter <= 1187, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")
scatter privDT year if ter > 1187, by(nameeng, note("")) c(l) xtitle("") ytitle("Estimated privatization effect")




